package com.open.party.cloud.view.examine

import android.os.Bundle
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.jiangyy.easydialog.CommonDialog
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.model.ExamineSignBean
import com.open.party.cloud.model.ExamineVo
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.viewModel.ExamineViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.examine_sign_up_activity.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ExamineSignUpActivity : AppEventBusTitleBaseActivity() {
    var viewModel: ExamineViewModel? = null
    private var examinationId: String? = null

    override fun getLayoutResId(): Int = R.layout.examine_sign_up_activity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("考试报名")
        examinationId = intent.getStringExtra("examinationId") as String

        viewModel = ExamineViewModel()

        submitBtn.setOnClickListener {
            submit()
        }
    }

    private fun submit() {
        val vo = ExamineVo()
        vo.examinationId = examinationId

        val userName = userNameEt.text.toString()
        if (XUtils.string().isEmpty(userName)) {
            XUtils.toast().show("请输入姓名")
            return
        }
        vo.userName = userName

        val phoneNum = phoneNumEt.text.toString()
        if (XUtils.string().isEmpty(phoneNum)) {
            XUtils.toast().show("请输入电话号码")
            return
        }
        if (phoneNum.length != 11 || '1' != phoneNum[0]) {
            XUtils.toast().show("电话号码可能不正确")
            return
        }
        vo.phone = phoneNum

        val unitName = unitEt.text.toString()
        if (XUtils.string().isEmpty(unitName)) {
            XUtils.toast().show("请输入单位")
            return
        }
        vo.unitName = unitName


        showLoadingDialog("正在提交")
        viewModel!!.examineSignUp(vo)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ExamineSignBean>) {
        if (result.eventType != "examineSignUp") {
            return
        }

        hideLoadingDialog()

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    CommonDialog.Builder(this)
                        .setCancelable(false)
                        .setTitle("报名成功")
                        .setMessage("准考证号：${result.data.examCode}\n\n温馨提示：请按时参加考试！")
                        .setPositiveButton("知道了") {
                            finish()
                        }.setNegativeButton("取消") {
                            finish()
                        }.show()
                } else {
                    XUtils.toast().show("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }
}